package com.cxl.demo.service;

import com.cxl.demo.mapper.AssigmentMapper;
import com.cxl.demo.mapper.CommentMapper;
import com.cxl.demo.mapper.UserMapper;
import com.cxl.demo.vo.Assigment;
import com.cxl.demo.vo.Comment;
import com.cxl.demo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService{

    @Autowired
    AssigmentMapper assigmentMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    User user;

    @Override
    public Integer addComment(Comment comment) {


            if(comment.getParentCommentId()!=null){

                Comment comment1=commentMapper.getCommentsByCommentId(comment.getParentCommentId());
                if(comment1.getTopId()!=null){
                    comment.setTopId(comment1.getTopId());
                }else{
                    comment.setTopId(comment1.getCommentId());
                }
            }
        commentMapper.add(comment);
        return 1;
    }

    @Override
    public Integer delComment(Integer commentId) {

        Comment comment=commentMapper.getCommentsByCommentId(commentId);
        if(comment==null){
            return 0;
        }
        Assigment assigment=assigmentMapper.findAssigmentById(comment.getAssigmentId());
        if(assigment==null){
            return 0;
        }
        Integer userId=user.getUserId();
        //不是管理员，不是任务发布人，不是评论发布人
        User user1=userMapper.findUserByUserId(user.getUserId());
        System.out.println(comment);
        System.out.println(assigment);
        System.out.println(user1);
        System.out.println(userId);

        if((userId.equals(comment.getUserId()))||(userId.equals(assigment.getUserId()))||(user1.getTypeId().equals(3))){
            commentMapper.DelCommentBuyId(commentId);
            return 1;

        }
        return 0;
    }

    @Override
    public List<Comment> getCommentsBuAssigmentId(Integer assigmentId) {
        List<Comment> list=commentMapper.getCommentsBuAssigmentId(assigmentId);

        return list;
    }


}
